import { routes } from '@/router/routes'
import { buildHierarchyTree } from '@/utils';
import { RouteRecordRaw, RouteComponent } from 'vue-router'
import { useMenuStore } from '@/store'
const store = useMenuStore()

const addRoute = store.menulist || []
function ascending(arr: any[]) {
  arr.forEach((v, index) => {
    if (!v.meta?.sort) v.meta.sort = index + 2;
  })
  const arrSort = arr.sort((a, b) => a?.meta.sort - b?.meta.sort);
  return arrSort;
}

function filterTree(data: RouteComponent[]) {
  const newTree = data.filter((v: any) => v.meta?.showLink !== false);
  newTree.forEach((v: any) => v.children && (v.children = filterTree(v.children)));
  return newTree;
}

export const constantRoutes: Array<RouteRecordRaw> = buildHierarchyTree(filterTree(ascending([...routes, ...addRoute])));

